package com.musifind.activities;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.musifind.common.GeneralHelper;
import com.musifind.utils.AppUtil;
import com.musifind.utils.SessionManager;
import com.musifind.wsclient.objects.User;
import com.musifind.wsclient.service.UserService;

public class RegisterActivity extends Activity implements OnClickListener {

	private EditText editName, editSurname, editEmail, editPassword, editBirthday;
	private Button btnRegister;
	private ToggleButton tglGender;
	private TextView txtToast;
	private String strName, strSurname, strEmail, strPassword, strBirthday, strGender;
	Calendar myCalendar;
	private SessionManager session;
	private View view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		session = new SessionManager(getApplicationContext());

		// activity starts as push old activity to left
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

		view = new View(getApplicationContext());

		btnRegister = (Button) findViewById(R.id.btnRegister);
		btnRegister.setOnClickListener(this);
		tglGender = (ToggleButton) findViewById(R.id.tglGender);
		editName = (EditText) findViewById(R.id.editName);
		editSurname = (EditText) findViewById(R.id.editSurname);
		editEmail = (EditText) findViewById(R.id.editEmail);
		editPassword = (EditText) findViewById(R.id.editPassword);
		editBirthday = (EditText) findViewById(R.id.editBirthday);

		tglGender.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					GeneralHelper.showRedToast(getApplicationContext(), "Kadın");
					strGender = "K";
				} else {
					GeneralHelper.showBlueToast(getApplicationContext(), "Erkek");
					strGender = "E";
				}
			}
		});

		myCalendar = Calendar.getInstance();
		final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				myCalendar.set(Calendar.YEAR, year);
				myCalendar.set(Calendar.MONTH, monthOfYear);
				myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
				updateBirthdayEditText();
			}

		};

		editBirthday.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new DatePickerDialog(RegisterActivity.this, date, myCalendar.get(Calendar.YEAR), myCalendar
						.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();
			}
		});

	}

	private void updateBirthdayEditText() {
		String myFormat = "dd/MM/yyyy";
		SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

		editBirthday.setText(sdf.format(myCalendar.getTime()));
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btnRegister) {
			if (editName.getText().length() == 0 || editSurname.getText().length() == 0
					|| editEmail.getText().length() == 0 || editPassword.getText().length() == 0
					|| editBirthday.getText().length() == 0) {
				GeneralHelper.showPurpleToast(getApplicationContext(), "Tüm alanların doldurulması zorunludur");
				return;
			}

			strName = editName.getText().toString();
			strSurname = editSurname.getText().toString();
			strEmail = editEmail.getText().toString();
			strPassword = editPassword.getText().toString();
			strBirthday = editBirthday.getText().toString();

			if (!AppUtil.isDevelopment()) {
				User responseUser = UserService.getUser(strEmail);
				if (responseUser == null) {
					session.setProperty(session.IS_FACE_USER, false);
					session.setProperty(session.USERNAME, strName);

					User user = new User();
					user.setAd(strName);
					user.setSoyad(strSurname);
					user.setMail(strEmail);
					user.setSifre(strPassword);
					// FIXME : dogum tarihi string degil date olacak
					user.setDogumTarih(new Date());
					user.setCinsiyet(strGender);
					user.setFaceUser(false);
					user.setGcmRegId(session.getGCMRegistrationId());
					User response = UserService.createUser(user);
					session.createUserId(response.getId());
				} else {
					session.setProperty(session.IS_FACE_USER, false);
					session.setProperty(session.USERNAME, strName);
					session.createUserId(responseUser.getId());

					Bitmap defaultImage = GeneralHelper.drawableToBitmap(getResources().getDrawable(
							R.drawable.pp_facebook));
					String strUserPicture = GeneralHelper.bitmapToString(defaultImage);
					session.setProperty(session.USER_PICTURE, strUserPicture);
				}
				GeneralHelper.showPurpleToast(getApplicationContext(), strName + "! Başarıyla kaydoldun.");
				Intent i = new Intent(RegisterActivity.this, HomepageActivity.class);
				startActivity(i);
			} else {
				Toast.makeText(RegisterActivity.this, "" + strName + "! Mail adresli Kullanıcı zaten mevcut.",
						Toast.LENGTH_SHORT).show();
			}
		}
	}
}
